package com.martin.fastframe.ui.penetrate

import android.view.MotionEvent
import com.martin.fastframe.R
import com.martin.fastframe.databinding.FragmentABinding
import com.martin.lib_base.base.BaseFragment
import com.martin.lib_base.common.CommonAdapter
import com.martin.lib_base.common.CommonVP2Adapter
import com.martin.lib_base.interfaces.IOnItemClickListener
import com.martin.lib_base.ktx.*
import com.xuexiang.xui.utils.ViewUtils

class AFragment : BaseFragment<FragmentABinding>() {

    private val fragmentB by lazy {
        BFragment()
    }

    override fun layoutId() = R.layout.fragment_a

    override fun initOnViewCreated() {
        binding.vp.adapter = CommonVP2Adapter(listOf(fragmentB), childFragmentManager, lifecycle)

        binding.v.setOnClickListener {
            loge("A 橙色 区域点击")
            showToast("A 橙色 区域点击")
        }
        binding.vBlue.setOnClickListener {
            loge("A 蓝色 区域点击")
            showToast("A 蓝色 区域点击")
        }

        binding.rv.adapter =
            CommonAdapter(requireContext(), listOf(PenetrateViewHolder::class.java))
        binding.rv.refresh(arrayListOf<Any>().apply {
            for (i in 0 until 9) {
                add(i)
            }
        })
        binding.rv.addOnItemClickListener(object : IOnItemClickListener {
            override fun onItemClick(position: Int, type: Int) {
                showToast("item 点击 $position")
            }
        })
    }

    /**
     * 内容触摸事件
     */
    fun onContentTouch(event: MotionEvent): Boolean {
        loge("内容触摸事件 A : ${event.action} ~ ${event.rawX},${event.rawY}")
        binding.rlContent.touchables.find {
            val rect = ViewUtils.calculateViewScreenLocation(it)
            event.rawX > rect.left && event.rawX < rect.right && event.rawY > rect.top && event.rawY < rect.bottom
        }?.let {
            return it.onTouchEvent(event)
        }
        return false
    }

}